授業計画をばらすPopup Menu
手作業で作るのがめんどいので作った
2022-04-29
10:19:33
TSにした
ページをbackgroundで作成するようにした
code:script.js
import { convert } from "./mod.ts";
scrapbox.PopupMenu.addButton({
title: 'class plan',
onClick: text => convert(text).text,
});
code:mod.ts
import { getIndentLineCount, getIndentCount } from "../scrapbox-userscript-std/text.ts";
import { makeSocket, patch } from "../scrapbox-userscript-std/mod.ts";
import { pool } from "../async-lib/mod.ts";
import { lightFormat } from "../date-fns/lightFormat.ts";
import type { Scrapbox } from "../scrapbox-jp%2Ftypes/userscript.ts";
declare const scrapbox: Scrapbox;
export const convert = (text: string): { promise: Promise<void>; text: string; } => {
// 最初の行を講義の短縮名として使う
const baseName = caption.trim();
// 最初の行と同じindent数の行を、切り出すブロックの先頭とする
const baseIndent = getIndentCount(caption);
const promise = (async () => {
const socket = await makeSocket();
await Promise.all([...pool(
3,
blocks,
(block) => patch(scrapbox.Project.name, ${baseName}-${block.index}, (lines) => [
...lines.map(line => line.text),
"目標",
...block.lines,
"",
"内容",
" ",
"",
lightFormat(new Date(), "#yyyy-MM-dd HH:mm:ss"),
], { socket }),
)]);
})();
return {
promise: null,
text: blocks.map((block) => [${baseName}-${block.index}]).join("\n"),
};
};
function* divide(lines: string[], baseIndent: number) {
for (let i = 0; i < lines.length; i++) {
if (getIndentCount(linesi) !== baseIndent) continue; const index = linesi.trim().split(".")0; if(isNaN(parseInt(index))) continue;
const count = getIndentLineCount(i, lines);
yield { lines: lines.slice(i, i + 1 + count), index: parseInt(index) };
i += count;
}
}